package com.example.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.ResponseResult;
import com.example.domain.entity.PlantCategory;
import com.example.exception.FloraException;
import com.example.service.IPlantCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zrr
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/plantCategory")
public class PlantCategoryController {
    @Autowired
    IPlantCategoryService plantCategoryService;
    @GetMapping("/getById/{id}")
    public ResponseResult getById(@PathVariable Long id){
        return ResponseResult.okResult(plantCategoryService.getPlantCategoryById(id));
    }

    @GetMapping("/getPlantCategory")
    public ResponseResult getAllPlantCategory(@RequestParam (defaultValue = "0")Integer pageNum, @RequestParam (defaultValue = "10")Integer pageSize){
        Page<PlantCategory> page=new Page<>(pageNum,pageSize);
        return ResponseResult.okResult(plantCategoryService.page(page));
    }

    @PostMapping("/add")
    public PlantCategory addPlantCategory(@RequestBody PlantCategory plantCategory)throws FloraException {
        return plantCategoryService.addPlantCategory(plantCategory);
    }

    @DeleteMapping("/deletePlantCategory/{id}")
    public void deletePlantCategory(@PathVariable Long id){
        plantCategoryService.removeById(id);
    }

    @PutMapping("/update")
    public PlantCategory updatePlantCategory(@RequestBody PlantCategory plantCategory)throws FloraException{
        return plantCategoryService.updatePlantCategory(plantCategory);
    }
}

